/*
 * @Descripttion:绘制
 * @version:
 * @Author: zhjs
 * @Date: 2023-8-24 09:49:46
 * @LastEditors: zhjs
 * @LastEditTime: 2023-8-24 09:49:46
 */

//=========================绘制==============================================
import * as Cesium from 'cesium'
class drawGraphic {
  //绘制线(贴地)
  static drawLine(viewer,handler) {
    // var handler = new Cesium.ScreenSpaceEventHandler(viewer.canvas)
    var positions = []
    var polyline = null
    var bufferedPolygon = undefined
    const _this = this
    var cartesian = null
    var PolyLinePrimitive = (function () {
      function _(positions) {
        this.options = {
          name: '直线',
          polyline: {
            show: true,
            positions: [],
            material: Cesium.Color.CHARTREUSE,
            width: 5,
            clampToGround: true
          }
        }
        this.positions = positions
        this._init()
      }

      _.prototype._init = function () {
        var _self = this
        var _update = function () {
          return _self.positions
        }
        //实时更新polyline.positions
        this.options.polyline.positions = new Cesium.CallbackProperty(
          _update,
          false
        )
        var addedEntity = viewer.entities.add(this.options)
      }
      return _
    })()
    // 监听鼠标移动事件
    handler.setInputAction(function (movement) {
      let ray = viewer.camera.getPickRay(movement.endPosition)
      cartesian = viewer.scene.globe.pick(ray, viewer.scene)
      if (!Cesium.defined(cartesian))
        //跳出地球时异常
        return

      if (positions.length > 1) {
        if (!Cesium.defined(polyline)) {
          polyline = new PolyLinePrimitive(positions)
          console.log(polyline)
        } else {
          positions.pop()
          positions.push(cartesian)
        }
      }
    }, Cesium.ScreenSpaceEventType.MOUSE_MOVE)
    handler.setInputAction(function (movement) {
      let ray = viewer.camera.getPickRay(movement.position)
      cartesian = viewer.scene.globe.pick(ray, viewer.scene)
      if (!Cesium.defined(cartesian))
        //跳出地球时异常
        return
      if (positions.length == 0) {
        positions.push(cartesian.clone())
      }
      positions.push(cartesian)
    }, Cesium.ScreenSpaceEventType.LEFT_CLICK)
    // 监听鼠标右键点击事件
    handler.setInputAction(function (movement) {
      handler.destroy() //关闭事件句柄
      handler = undefined
      positions.pop() //最后一个点无效
      if (positions.length == 1)
        viewer.entities.remove(floatingPoint)

    }, Cesium.ScreenSpaceEventType.RIGHT_CLICK)
    return positions
  }

}
export default drawGraphic;
